package learn.advance.exception1;

import com.sun.tools.javac.parser.Parser;
import com.sun.tools.javah.Gen;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

class Demo1Exception {
    public static void Test() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = null;

        try {
            date = sdf.parse("1999-09");
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println("hello");
    }
}

class Demo2Throw {
    public static void Test() {
        int[] arr = new int[3];
        arr[0] = 123;
        arr[1] = 234;
        arr[2] = 333;

        int e = GetElement(arr, -1);
        System.out.println(e);
    }

    public static int GetElement(int[] arr, int index) {
        if (arr == null) {
            throw new NullPointerException("传递的数组是空");
        }

        if (index < 0 || index > arr.length - 1) {
            throw new ArrayIndexOutOfBoundsException("传递的索引超除了数组范围");
        }

        int ele = arr[index];
        return ele;
    }
}

class Demo3Objects {
    public static void Test() {
        Method(new Object());
    }

    private static void Method(Object o) {
        Objects.requireNonNull(o, "传递的对象是kull");
    }
}

class Demo1TryCatch {
    public static void Test() {
        try {
            ReadFile("w.w");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally");
        }
        System.out.println("后续代码");
    }

    private static void ReadFile(String file_name) throws IOException {
        if (!file_name.endsWith(".txt")) {
            throw new IOException("文件后缀名错误");
        }
        System.out.println("路径没有问题");
    }
}


/*
    子父类的异常:
        - 如果父类抛出了多个异常,子类重写父类方法时,抛出和父类相同的异常或者是父类异常的子类或者不抛出异常。
        - 父类方法没有抛出异常，子类重写父类该方法时也不可抛出异常。此时子类产生该异常，只能捕获处理，不能声明抛出
    注意:
        父类异常时什么样,子类异常就什么样
 */

class Fu {
    public void Show1() throws NullPointerException, ClassNotFoundException {
    }

    public void Show2() throws IndexOutOfBoundsException {
    }

    public void Show3() throws IndexOutOfBoundsException {
    }

    public void Show4() {
    }
}

class Zi extends Fu {
    @Override
    public void Show1() throws NullPointerException, ClassNotFoundException {
    }

    @Override
    public void Show2() throws IndexOutOfBoundsException {
    }

    @Override
    public void Show3() {
    }

    @Override
    public void Show4()
    //throws Exception
    {
        int a = 10;
        //throw new Exception("异常");
    }
}

class Demo03Exception {
    public static void Test() {
        Zi zi = new Zi();

    }
}


public class TestException {
    public static void main(String[] args) {
        //Demo1Exception.Test();
        //Demo2Throw.Test();
        //Demo3Objects.Test();
        //Demo1TryCatch.Test();

        Demo03Exception.Test();
    }
}
